<extend name="Public:base" />
<block name="content"> 
	<div id="cart">
		<div class="container">
			<div class="row">
				<div class="clearfix col-md-12">
				<div class="page-title">
					<h1>购物车  (<span id="weight">{$weight}</span><?php echo get_weight_name(C('WEIGHT_ID')); ?> ) </h1>
				</div>
				
					<div class="alert alert-danger hidden" role="alert">
					  <button type="button" class="close"><span aria-hidden="true">&times;</span></button>
					  <div id="fail"></div>
					</div>
				
				<form>
					<div class="table-responsive">
						<table class="table table-bordered">
							<thead>
								<tr>									
									<td  colspan="2" class="text-left">商品名称</td>
									<td class="text-left">型号</td>
									<td class="text-left">数量</td>
									<td class="text-left">单价</td>
									<td class="text-left">总计</td>
									<td class="remove"></td>
								</tr>
							</thead>
							
							<tbody>
								<volist name="goods" id="d">
									<tr class="goods<?php echo md5($key); ?>">
										<td style="width:100px;padding:20px;"><a href="{:U('/goods/'.$d['goods_id'])}"><img src="__ROOT__/{$d.image}" /></a></td>
										<td >{$d.name}
											<?php if (!$d['stock']) { ?>
											  <span class="stock">***</span>
											  <?php } ?>
											  <div>
												<?php foreach ($d['option'] as $option) { ?>
												<small><?php echo $option['name']; ?>: <?php echo $option['value']; ?></small><br />
												<?php } ?>
							                
											  </div>
										</td>
										<td>{$d.model}</td>
										<td class="quantity" style="width:200px;">
											<div id="select_number<?php echo md5($key); ?>" class="select_number">
											  <input class="text" type="text" name="quantity{$d.goods_id}"  onkeyup='change_quantity("{$d.goods_id}",this,"{$key}","<?php echo md5($key); ?>");' value="{$d.quantity}" size="1" />
												<div>
													<button onclick='changeQty(1,"{$d.goods_id}","{$key}","<?php echo md5($key); ?>"); return false;' class="increase">+</button>
													<button onclick='changeQty(0,"{$d.goods_id}","{$key}","<?php echo md5($key); ?>"); return false;' class="decrease">-</button>
												</div>
											</div>						
										</td>		
										
										<td class="price">￥<?php echo round($d['price'] ,2); ?></td>
										<td class="total">￥<?php echo round($d['total'] ,2); ?></td>
										
										<td>
											<a href="{:U('/remove/'.$key)}">
												<img class="btooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="删除" src="__IMG__/remove.png" alt="删除" title="删除" />
											</a>
										</td>
									</tr>
								</volist>
							</tbody>
							
						</table>
					</div>
				</form>				
				 <div class="cart-total ">
				    <table id="total">				      
				      <tr>				
				        <td class="right price last">总计：<?php echo '￥'.$total_price; ?></td>
				      </tr>				      
				    </table>
				 </div>
			     <div class="buttons cart-buttons">					  
				      <a href="{:U('/checkout')}" class="btn btn-primary btn-checkout">结算</a>
				      <a href="__ROOT__/" class="btn btn-primary btn-continue">继续购物</a>
			     </div>  
				</div>				
			</div>
		</div>
	</div>
</block>
<block name="foot_script">
<script>	
{/* id:商品id,qty:数量,option:商品选项,key:用于选择标签 */}
function update_cart(id,qty,option,key){
	$.post(
		'{:U("/cart_update")}',
		{id:id,q:qty,o:option},
		function(json){
		
			$('.alert').addClass('hidden');
			
			if (json['success']) {				
				
				$('#cart-total').text(json['success']);
				
				$('#select_number' + key).find("input").val(qty);
				
				$('.goods' + key).find("td.price").text('￥ '+json['price']);
				
				$('.goods' + key).find("td.total").text('￥ '+json['total_price']);
				
				$('.last').text('总计： ￥ '+json['total_all_price']);
				
				$('#weight').text(json['weight']);
				
			}else if(json['error']){
				
				$('.alert-danger').removeClass('hidden');
				
				$('#fail').text(json['error']);				
				
			}	
		}
	);
}

function change_quantity(key,input,option,key){
	var qty=input.value;	
	update_cart(key,qty,option,key);
}

function changeQty(increase,goods_id,option,key) {
    var qty = parseInt($('#select_number' + key).find("input").val());  

    if ( !isNaN(qty) ) {
    	//增加
		if(increase){			
			update_cart(goods_id,(qty+1),option,key);
		}else{
			update_cart(goods_id,(qty-1),option,key);
		}     
    }
}	
$('.close').click(function(){
		$(this).parent().addClass('hidden');
});
</script>
</block>